package com.servicesystem.demo.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName TestController
 * @Description TODO
 * @Author Mike
 * @Date 2019/6/18 11:10
 * @Version 1.0
 */
@CrossOrigin
@RestController
public class TestController {


    @RequestMapping("/test")
    public String test(){
        return "hello world";
    }

    @PostMapping("/post")
    public JSONObject post(HttpServletRequest request){
        InputStream is = null;
        try
        {
            is = request.getInputStream();
            StringBuilder sb = new StringBuilder();
            byte[] b = new byte[4096];
            for (int n; (n = is.read(b)) != -1;)
            {
                sb.append(new String(b, 0, n));
            }
            System.out.println(sb.toString());
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/origin")
    public JSONObject origin(@RequestBody JSONObject json){
        System.out.println(json);
        return json;
    }
}
